﻿using WebOptimizer.Net.Services.V1;

namespace WebOptimizer.TestClient
{
	class Program
	{
		#region Main

		static void Main(string[] args)
		{
			TaskCollection items;
			Task item;
			Source source;
			string fileContent;

			try
			{
				items = null;
				if ((args == null) || (args.Length < 2))
				{
					// Read file
					if (Sidvall.SystemContext.Current.FileSystemManager.FileExistsAsync(WebOptimizer.SystemContext.Current.ConfigurationContext.ConfigurationSettings.DataFilePath).Result)
					{
						fileContent = Sidvall.SystemContext.Current.FileSystemManager.ReadAllTextAsync(WebOptimizer.SystemContext.Current.ConfigurationContext.ConfigurationSettings.DataFilePath).Result;
						items = Sidvall.SystemContext.Current.Serializer.Deserialize<TaskCollection>(fileContent);
					}
					else
					{
						System.Console.WriteLine("Usage 1:");
						System.Console.WriteLine("WebOptimizer.TestClient.exe js <path to destination file> <path1 to source files> <path2 to source files> <path3 to source files> etc");
						System.Console.WriteLine();
						System.Console.WriteLine("Usage 2:");
						System.Console.WriteLine("WebOptimizer.TestClient.exe css <path to destination file> <path1 to source files> <path2 to source files> <path3 to source files> etc");
						System.Console.WriteLine();
						System.Console.WriteLine("Usage 3:");
						System.Console.WriteLine("WebOptimizer.TestClient.exe config <path to configuration file>");
						System.Console.WriteLine("You can create configuration files using the windows client.");
						System.Console.ReadLine();
					}
				}
				else
				{
					switch (args[0].ToUpperInvariant())
					{
						case "CONFIG":
							if (Sidvall.SystemContext.Current.FileSystemManager.FileExistsAsync(args[1]).Result)
							{
								fileContent = Sidvall.SystemContext.Current.FileSystemManager.ReadAllTextAsync(args[1]).Result;
								items = Sidvall.SystemContext.Current.Serializer.Deserialize<TaskCollection>(fileContent);
							}
							break;
						case "CSS":
							items = new TaskCollection();
							item = new Task()
							{
								TaskTypeId = Data.TaskType.CompressCss,
								IsEnabled = true,
								Path = args[1],
							};
							item.Sources = new SourceCollection();
							for (int i = 2; i < args.Length; i++)
							{
								source = new Source()
								{
									IgnoredFiles = ".min.css",
									IncludeSubfolders = true,
									IsEnabled = true,
									SearchPatterns = "*.css",
									Path = args[i],
								};
								item.Sources.Add(source);
							}
							items.Add(item);
							break;
						case "JS":
							items = new TaskCollection();
							item = new Task()
							{
								TaskTypeId = Data.TaskType.CompressJavaScript,
								IsEnabled = true,
								Path = args[1],
							};
							item.Sources = new SourceCollection();
							for (int i = 2; i < args.Length; i++)
							{
								source = new Source()
								{
									IgnoredFiles = ".intellisense.js|.debug.js|.min.js",
									IncludeSubfolders = true,
									IsEnabled = true,
									SearchPatterns = "*.js",
									Path = args[i],
								};
								item.Sources.Add(source);
							}
							items.Add(item);
							break;
						default:
							break;
					}
				}
				if (items != null)
				{
					ExecuteTasks(items);

					System.Console.WriteLine();
					System.Console.WriteLine("Done.");
				}
			}
			catch (System.Exception ex)
			{
				System.Console.WriteLine(ex.ToString());
				System.Console.ReadLine();
			}
		}

		#endregion
		#region ExecuteTasks

		private static void ExecuteTasks(TaskCollection items)
		{
			WebOptimizer.Workflow.ExecuteTaskJob manager;

			manager = new Workflow.ExecuteTaskJob();
			foreach (var item in items)
			{
				System.Console.WriteLine("Creating: " + item.Path);
				manager.Settings = Sidvall.SystemContext.Current.Serializer.Serialize(item);
				manager.ExecuteAsync().Wait();
			}
		}

		#endregion
	}
}
